package com.wangtao.firstapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.wangtao.firstapp.R;
import com.wangtao.firstapp.adapters.HotRecommendAdapter;
import com.wangtao.firstapp.modles.HotRecommendCommentData;
import com.wangtao.firstapp.modles.HotRecommendData;
import com.wangtao.firstapp.utils.HttpUtils;

/**
 * 晒单页面下热门推荐二级页面
 * Created by lanouhn on 16/9/8.
 */
public class HotRecommendActivity extends AppCompatActivity {
    private ImageView iv_back_titlebar_hotrecommend;// 标题返回按钮
    private TextView tv_comments_hotrecommend;
    private RecyclerView recyclerview_hotrecommend;
    private HotRecommendData hotRecommendData;// 热门推荐二级页面实体类
    private HotRecommendCommentData hotRecommendCommentData;// 热门推荐二级页面中的评论实体类
    private HotRecommendAdapter hotRecommendAdapter;// 二级页面适配器
    private String addid = "";
    private LinearLayout linearlayout_tags_hotrecommend;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hotrecommend);
        initView();
        initData();
        initEvent();
    }

    private void initView() {
        iv_back_titlebar_hotrecommend = (ImageView) findViewById(R.id.iv_back_titlebar_hotrecommend);
        iv_back_titlebar_hotrecommend = (ImageView) findViewById(R.id.iv_back_titlebar_hotrecommend);
        recyclerview_hotrecommend = (RecyclerView) findViewById(R.id.recyclerview_hotrecommend);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(HotRecommendActivity.this);
        recyclerview_hotrecommend.setLayoutManager(linearLayoutManager);
    }

    private void initData() {

        String url = "http://open4.bantangapp.com/community/post/hotRecommend";
        String params = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2559956&oauth_token=6beffa8c92276d19d3c23acb596024e6&track_deviceid=866962021116243&track_device_info=m1+note&channel_name=ppzhushuo&app_installtime=1471923362&app_versions=5.8.6&os_versions=5.1&screensize=1080&v=18&page=0&pagesize=18&id=1&type_id=1";
        try {
            HttpUtils.doPostAsyn(url, params, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson = new Gson();
                    hotRecommendData = gson.fromJson(result, HotRecommendData.class);
                    if (hotRecommendData.getData().size() > 0) {
                        handler.sendEmptyMessage(1);
                    } else {
                        handler.sendEmptyMessage(0);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

        //addid = bundle.getString("addid");
        String url1 = "http://open4.bantangapp.com/post/comment/list";
        String param1 = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2559956&oauth_token=6beffa8c92276d19d3c23acb596024e6&track_deviceid=866962021116243&track_device_info=m1+note&channel_name=meizu&app_installtime=1471923362&app_versions=5.8.7&os_versions=5.1&screensize=1080&v=19&page=0&pagesize=10&id=" + addid;
        try {
            HttpUtils.doPostAsyn(url1, param1, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson1 = new Gson();
                    hotRecommendCommentData = gson1.fromJson(result, HotRecommendCommentData.class);
                    if (hotRecommendCommentData.getData().size() > 0) {
                        handler.sendEmptyMessage(2);
                    } else {
                        // handler.sendEmptyMessage(0);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initEvent() {
        iv_back_titlebar_hotrecommend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    // Toast.makeText(HotRecommendActivity.this, "网络访问失败", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    Bundle bundle = getIntent().getExtras();
                    int position = bundle.getInt("position");
                    hotRecommendAdapter = new HotRecommendAdapter(HotRecommendActivity.this, hotRecommendData);
                    recyclerview_hotrecommend.setAdapter(hotRecommendAdapter);
                    recyclerview_hotrecommend.scrollToPosition(position);
//                    View view = LayoutInflater.from(HotRecommendActivity.this).inflate(R.layout.item_rv_hotrecommend, null);
//                    linearlayout_tags_hotrecommend = (LinearLayout) view.findViewById(R.id.linearlayout_tags_hotrecommend);
//                    for (int i = 0; i < hotRecommendData.getData().size(); i++) {
//                        for (int j = 0; j < hotRecommendData.getData().get(i).getTags().size(); j++) {
//                            TextView textView_tags = new TextView(HotRecommendActivity.this);
//                            textView_tags.setText(hotRecommendData.getData().get(j).getTags().get(j).getName());
//                            linearlayout_tags_hotrecommend.addView(textView_tags);
//                        }
//                    }
                    break;
                case 2:
                    View view1 = LayoutInflater.from(HotRecommendActivity.this).inflate(R.layout.item_rv_hotrecommend, null);
                    tv_comments_hotrecommend = (TextView) view1.findViewById(R.id.tv_comments_hotrecommend);
                    if (hotRecommendCommentData.getData() != null) {
                        tv_comments_hotrecommend.setText("所有" + hotRecommendCommentData.getData().size() + "条评论");
                    }
                    break;
            }
        }
    };

}
